package audio;

import ddf.minim.AudioPlayer;
import ddf.minim.AudioSnippet;
import ddf.minim.Minim;
import processing.core.PApplet;


public class Audio {
	
	public static final String ROOT_DIR = "sound/";
	
	private static Audio	_instance = null;
	private static Minim	_minim;
	
	private static String[] _srcBlub = new String[] {	"blub01.wav", 
														"blub02.wav", 
														"blub03.wav", 
														"blub04.wav", 
														"blub05.wav", 
														"blub06.wav", 
														"blub07.wav"};
	private static String[] _srcPuck = new String[] {	"puck01.wav", 
														"puck02.wav", 
														"puck03.wav"};
	private static String[] _srcWigg = new String[] {	"wiggle01.wav", 
														"wiggle02.wav", 
														"wiggle03.wav", 
														"wiggle04.wav"};
	
	private static AudioSnippet[] _snpBlub;
	private static AudioSnippet[] _snpPuck;
	private static AudioSnippet[] _snpWigg;
	
	protected Audio () {
	}
	
	public static void init (PApplet theApplet) {
		getInstance ();
		
		_minim = new Minim (theApplet);
		
		_snpBlub = new AudioSnippet[_srcBlub.length];
		_snpPuck = new AudioSnippet[_srcPuck.length];
		_snpWigg = new AudioSnippet[_srcWigg.length];
		
		for (int i=0; i < _srcBlub.length; i++) {
			_snpBlub[i] = _minim.loadSnippet (ROOT_DIR + _srcBlub[i]);
		}
		
		for (int i=0; i < _srcPuck.length; i++) {
			_snpPuck[i] = _minim.loadSnippet (ROOT_DIR + _srcPuck[i]);
		}
		
		for (int i=0; i < _srcWigg.length; i++) {
			_snpWigg[i] = _minim.loadSnippet (ROOT_DIR + _srcWigg[i]);
		}
		
		AudioPlayer bg1 = _minim.loadFile (ROOT_DIR + "background01.wav");
		AudioPlayer bg2 = _minim.loadFile (ROOT_DIR + "background02.wav");
		AudioPlayer bg3 = _minim.loadFile (ROOT_DIR + "background03.wav");
		
		bg1.setGain (-21);
		
		bg1.loop ();
		bg2.loop ();
		bg3.loop ();
	}
	
	public static Audio getInstance () {
		if (_instance == null) {
			_instance = new Audio ();
		}
		return _instance;
	}
	
	public static void playBlub () {
		playBlub ((int) Math.floor ((_snpBlub.length - 1) * Math.random ()));
	}
	
	public static void playBlub (int theIndex) {
		_snpBlub[theIndex].rewind ();
		_snpBlub[theIndex].play ();
	}
	
	public static void playPuck () {
		playPuck ((int) Math.floor ((_snpPuck.length - 1) * Math.random ()));
	}
	
	public static void playPuck (int theIndex) {
		_snpPuck[theIndex].rewind ();
		_snpPuck[theIndex].play ();
	}
	
	public static void playWiggle () {
		playWiggle ((int) Math.floor ((_snpWigg.length - 1) * Math.random ()));
	}
	
	public static void playWiggle (int theIndex) {
		_snpWigg[theIndex].rewind ();
		_snpWigg[theIndex].play ();
	}
	
	public static int numBlub () {
		return _snpBlub.length;
	}
	
	public static int numPuck () {
		return _snpPuck.length;
	}
	
	public static int numWiggle () {
		return _snpWigg.length;
	}
	
	public static AudioSnippet getSnippet (String theFilename) {
		return _minim.loadSnippet (ROOT_DIR + theFilename);
	}
}